//将SDL函数和数据类型包含进来
#include "SDL/SDL.h"
#include <string>
#include <iostream>

using namespace std;

#define UNUSED(x) (void)x
//窗口属性
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

static int n = 0;
//保护性的信号量
SDL_sem *nLock = NULL;

//退出标识
bool quit = false;
//将要用到的事件结构体
SDL_Event event;

SDL_Surface *load_image( std::string filename ) 
{
    //临时的空间，用于存储刚刚加载好的图像
    SDL_Surface* loadedImage = NULL;
    //优化后的图像，实际使用的是这个图像
    SDL_Surface* optimizedImage = NULL;

    //加载图像
    loadedImage = SDL_LoadBMP( filename.c_str() );

    //如果加载图片没有出错
    if( loadedImage != NULL )
    {
        //创建一个优化了的图像
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        //释放临时的图像
        SDL_FreeSurface( loadedImage );
    }

    //返回优化后的表面
    return optimizedImage;
}


/**
 * @brief 
 * 
 * @param x 
 * @param y 
 * @param source : Blit的目标表面
 * @param destination : 保存了Blit的目标位置
 */
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //新建一个临时的矩形来保存偏移量
    SDL_Rect offset;
    
    //将传入的偏移量保存到矩形中
    offset.x = x;
    offset.y = y;
    //执行表面的Blit
    SDL_BlitSurface( source, NULL, destination, &offset );
}

int my_thread1( void *data )
{
    (void)data;
    //当程序还未结束
    while( quit == false )
    {
        //锁定
        SDL_SemWait( nLock ); //保证n改变后，第一次是在本函数中输出
        
        n++;
        SDL_Delay( 500 );
        cout << "my_thread1: " << n << endl;

        //解锁
        SDL_SemPost( nLock );
        
        SDL_Delay( 500 );
    }
    
    return 0;    
}

int my_thread2( void *data )
{
    (void)data;
    //当程序还未结束
    while( quit == false )
    {
         //锁定
        SDL_SemWait( nLock ); //保证n改变后，第一次是在本函数中输出
        
        n++;
        SDL_Delay( 500 );
        cout << "my_thread2: " << n << endl;

        //解锁
        SDL_SemPost( nLock );
        
        SDL_Delay( 500 );

    }
    
    return 0;    
}

int main( int argc, char* args[] ) 
{ 
    UNUSED(argc);
    UNUSED(args);

    //将要使用的表面
    SDL_Surface *message = NULL;
    SDL_Surface *background = NULL;
    SDL_Surface *screen = NULL;
    
    //将要被使用的线程
    SDL_Thread *thread1 = NULL;
    SDL_Thread *thread2 = NULL;

    //启动SDL
    SDL_Init( SDL_INIT_EVERYTHING );
    //设置窗口
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    //设置窗口标题
    SDL_WM_SetCaption( "SDL HelloWorld", NULL );

    //创建信号量
    nLock = SDL_CreateSemaphore( 1 );

    //创建并运行线程
    thread1 = SDL_CreateThread( my_thread1, NULL );
    thread2 = SDL_CreateThread( my_thread2, NULL );

    //加载图片
    message = load_image( "../assert/1.bmp" );
    background = load_image( "../assert/background.bmp" );

    //将背景图片background应用到screen上
    apply_surface( 0, 0, background, screen );
    apply_surface( 320, 0, background, screen );
    apply_surface( 0, 240, background, screen );
    apply_surface( 320, 240, background, screen );

    //设置表面的alpha
    SDL_SetAlpha( message, SDL_SRCALPHA, 128 );
    //将message表面应用到窗口中
    apply_surface( 160, 140, message, screen );

    //更新窗口
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;    
    }
    
    //当用户还未退出
    while( quit == false )
    {
        //当有事件需要处理
        while( SDL_PollEvent( &event ) )
        {
            //如果用户叉掉了窗口
            if( event.type == SDL_QUIT )
            {
                //退出程序
                quit = true;
            }    
        }
        SDL_Delay( 20 );
    }

    //停止线程
    SDL_KillThread( thread1 );
    SDL_KillThread( thread2 );
    //释放表面
    SDL_FreeSurface( message );
    SDL_FreeSurface( background );

    //退出SDL
    SDL_Quit();

    //main函数返回
    return 0;
}